package com.remote.ui.controller;

import com.remote.common.command.Commands;
import com.remote.common.constant.MessageConstants;
import com.remote.common.exception.RemoteException;
import com.remote.common.model.ClientInfo;
import com.remote.common.model.Request;
import com.remote.common.model.Response;
import com.remote.common.tool.MachineTool;
import com.remote.common.util.IPUtil;
import com.remote.ui.constant.UIConstant;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.StringUtil;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import java.util.HashMap;
import java.util.Map;

public class MainController extends BaseController {
    
    public Label localHost;
    public TextField remoteHost;
    public TextArea console;


    @Override
    protected void initView(Scene scene, Map<String, Object> vars) {

    }

    @Override
    protected void initEvent(Scene scene, Map<String, Object> vars) {

    }


    public void connectAction(ActionEvent actionEvent) {
        try {
            this.console.appendText("正在连接服务器\n");
            Channel connect = getControllerManager().getCommandContext().connect();

            Request request = new Request();
            request.setCommand(Commands.CONNECT);
            ClientInfo clientInfo = new ClientInfo();
            clientInfo.setMacAddress(IPUtil.getMAC());
            clientInfo.setOsName(System.getProperty("os.name"));
            clientInfo.setUserHostName(System.getProperty("user.name"));
            clientInfo.setOsName(System.getProperty("os.name"));
            clientInfo.setInteriorIp(IPUtil.getIntranetIp());
            clientInfo.setSupportableDpi(MachineTool.getInstance().getSupportableDPIs());
            request.setValue(clientInfo);
            connect.writeAndFlush(request);

        } catch (Exception e) {
            e.printStackTrace();
            showMessage(MessageConstants.CONNECT_SERVER_FAILED, e.getMessage());
        }
    }

    public void remoteAction(ActionEvent actionEvent) {
        if (StringUtil.isNullOrEmpty(remoteHost.getText())) {
            showMessage(MessageConstants.CLIENT_ID_EMPTY);
        } else if (remoteHost.getText().equals(this.getControllerManager().getCommandContext().getClient().getClientId())) {
            showMessage(MessageConstants.CANT_CONNECT_TO_ITSELF);
        } else {
            try {
                console.appendText("正在连接目标主机\n");
                getControllerManager().getCommandContext().fireCommand(remoteHost.getText(), Commands.CONTROL, null);
            } catch (RemoteException e2) {
                showMessage(e2.getMessage());
            }
        }
    }

    @Override
    protected void onReadDate(ChannelHandlerContext ctx, Object obj) {
        Response response = (Response) obj;
        if (Boolean.FALSE.equals(response.getIsSucceed())) {
            console.appendText((String) response.getValue() + "\n");
            return;
        }
        switch ((Commands) response.getCommand()) {
            case CONNECT:
                this.connect(ctx, response);
                break;
            case CONTROL:
                this.control(response);
                break;
            default:
                break;
        }


    }

    private void control(Response response) {
        if (response.getValue() != null) {
            if (response.getValue() instanceof ClientInfo) {
                try {
                    //获取客户端信息传到下一个界面
                    ClientInfo clientInfo = (ClientInfo) response.getValue();
                    Map<String, Object> vars = new HashMap<>();
                    vars.put(UIConstant.P_CLIENT_INFO, clientInfo);
                    getControllerManager().getStarter(UIConstant.UI_SCREEN).show(response.getTargetClientId(), vars);
                } catch (Exception e) {
                    e.printStackTrace();
                    showMessage(e.getMessage());
                }
                console.appendText("连接目标主机成功：" + response.getTargetClientId() + "\n");
            } else {
                showMessage(MessageConstants.CONNECT_HOST_FAILED);
            }
        } else {
            console.appendText(response.getClientId() + "正在连接你" + "\n");
        }
    }

    private void connect(ChannelHandlerContext ctx, Response response) {
        getControllerManager().getCommandContext().setContext(ctx);
        getControllerManager().getCommandContext().setClient((ClientInfo) response.getValue());
        localHost.setText(response.getClientId());
        console.appendText("连接服务器成功：" + response.getClientId() + "\n");
    }


}
